home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / hplip / base / g.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  12KB  |  306 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import os.path as os
  7. import ConfigParser
  8. import locale
  9. import pwd
  10. import stat
  11. import re
  12. from codes import *
  13. import logger
  14. log = logger.Logger('', logger.Logger.LOG_LEVEL_INFO, logger.Logger.LOG_TO_CONSOLE)
  15. log.set_level('info')
  16. MINIMUM_PYQT_MAJOR_VER = 3
  17. MINIMUM_PYQT_MINOR_VER = 14
  18. MINIMUM_QT_MAJOR_VER = 3
  19. MINIMUM_QT_MINOR_VER = 0
  20.  
  21. def to_bool(s, default = False):
  22.     None if isinstance(s, str) and s else s[0].lower() in ('1', 't', 'y')
  23.     return default
  24.  
  25.  
  26. class Properties(dict):
  27.     
  28.     def __getattr__(self, attr):
  29.         if attr in self.keys():
  30.             return self.__getitem__(attr)
  31.         return ''
  32.  
  33.     
  34.     def __setattr__(self, attr, val):
  35.         self.__setitem__(attr, val)
  36.  
  37.  
  38. prop = Properties()
  39.  
  40. class ConfigBase(object):
  41.     
  42.     def __init__(self, filename):
  43.         self.filename = filename
  44.         self.conf = ConfigParser.ConfigParser()
  45.         self.read()
  46.  
  47.     
  48.     def get(self, section, key, default = u''):
  49.         
  50.         try:
  51.             return self.conf.get(section, key)
  52.         except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
  53.             return default
  54.  
  55.  
  56.     
  57.     def set(self, section, key, value):
  58.         if not self.conf.has_section(section):
  59.             self.conf.add_section(section)
  60.         
  61.         self.conf.set(section, key, value)
  62.         self.write()
  63.  
  64.     
  65.     def sections(self):
  66.         return self.conf.sections()
  67.  
  68.     
  69.     def has_section(self, section):
  70.         return self.conf.has_section(section)
  71.  
  72.     
  73.     def options(self, section):
  74.         return self.conf.options(section)
  75.  
  76.     keys = options
  77.     
  78.     def read(self):
  79.         if self.filename is not None:
  80.             filename = self.filename
  81.             if filename.startswith('/root/'):
  82.                 log.error("attempted to read from '%s'" % self.filename)
  83.                 return None
  84.             
  85.             try:
  86.                 fp = open(self.filename, 'r')
  87.                 self.conf.readfp(fp)
  88.                 fp.close()
  89.             except (OSError, IOError):
  90.                 filename.startswith('/root/')
  91.                 filename.startswith('/root/')
  92.                 log.debug('Unable to open file %s for reading.' % self.filename)
  93.             except:
  94.                 filename.startswith('/root/')<EXCEPTION MATCH>(OSError, IOError)
  95.             
  96.  
  97.         filename.startswith('/root/')
  98.  
  99.     
  100.     def write(self):
  101.         if self.filename is not None:
  102.             filename = self.filename
  103.             if filename.startswith('/root/') or filename.startswith('/etc/'):
  104.                 log.error("attempted to write to '%s'" % self.filename)
  105.                 return None
  106.             
  107.             try:
  108.                 fp = open(self.filename, 'w')
  109.                 self.conf.write(fp)
  110.                 fp.close()
  111.             except (OSError, IOError):
  112.                 filename.startswith('/etc/')
  113.                 filename.startswith('/etc/')
  114.                 log.debug('Unable to open file %s for writing.' % self.filename)
  115.             except:
  116.                 filename.startswith('/etc/')<EXCEPTION MATCH>(OSError, IOError)
  117.             
  118.  
  119.         filename.startswith('/etc/')
  120.  
  121.  
  122.  
  123. class SysConfig(ConfigBase):
  124.     
  125.     def __init__(self):
  126.         ConfigBase.__init__(self, '/etc/hp/hplip.conf')
  127.  
  128.  
  129.  
  130. class State(ConfigBase):
  131.     
  132.     def __init__(self):
  133.         ConfigBase.__init__(self, '/var/lib/hp/hplip.state')
  134.  
  135.  
  136.  
  137. class UserConfig(ConfigBase):
  138.     
  139.     def __init__(self):
  140.         if not os.geteuid() == 0:
  141.             prop.user_dir = os.path.expanduser('~/.hplip')
  142.             
  143.             try:
  144.                 if not os.path.exists(prop.user_dir):
  145.                     os.makedirs(prop.user_dir)
  146.             except OSError:
  147.                 pass
  148.  
  149.             prop.user_config_file = os.path.join(prop.user_dir, 'hplip.conf')
  150.             if not os.path.exists(prop.user_config_file):
  151.                 
  152.                 try:
  153.                     file(prop.user_config_file, 'w').close()
  154.                     s = os.stat(os.path.dirname(prop.user_config_file))
  155.                     os.chown(prop.user_config_file, s[stat.ST_UID], s[stat.ST_GID])
  156.                 except IOError:
  157.                     pass
  158.                 except:
  159.                     None<EXCEPTION MATCH>IOError
  160.                 
  161.  
  162.             None<EXCEPTION MATCH>IOError
  163.             ConfigBase.__init__(self, prop.user_config_file)
  164.         else:
  165.             prop.user_dir = None
  166.             prop.user_config_file = None
  167.             ConfigBase.__init__(self, None)
  168.  
  169.     
  170.     def workingDirectory(self):
  171.         t = self.get('last_used', 'working_dir', os.path.expanduser('~'))
  172.         
  173.         try:
  174.             t = t.decode('utf-8')
  175.         except UnicodeError:
  176.             log.error('Invalid unicode: %s' % t)
  177.  
  178.         log.debug('working directory: %s' % t)
  179.         return t
  180.  
  181.     
  182.     def setWorkingDirectory(self, t):
  183.         self.set('last_used', 'working_dir', t.encode('utf-8'))
  184.         log.debug('working directory: %s' % t.encode('utf-8'))
  185.  
  186.  
  187. os.umask(31)
  188. sys_conf = SysConfig()
  189. sys_state = State()
  190. user_conf = UserConfig()
  191.  
  192. try:
  193.     (prop.locale, prop.encoding) = locale.getdefaultlocale()
  194. except ValueError:
  195.     prop.locale = 'en_US'
  196.     prop.encoding = 'UTF8'
  197.  
  198. prop.version = sys_conf.get('hplip', 'version', '0.0.0')
  199. _p = re.compile('(\\d\\w*)', re.I)
  200. _x = []
  201. for _y in prop.version.split('.')[:3]:
  202.     _z = _p.match(_y)
  203.     if _z is not None:
  204.         _x.append(_z.group(1))
  205.         continue
  206.  
  207. prop.installed_version = '.'.join(_x)
  208.  
  209. try:
  210.     prop.installed_version_int = []([]([ '%02x' % int(_y) for _y in _x ]), 16)
  211. except ValueError:
  212.     prop.installed_version_int = 0
  213.  
  214. prop.home_dir = sys_conf.get('dirs', 'home', os.path.realpath(os.path.normpath(os.getcwd())))
  215. prop.username = pwd.getpwuid(os.getuid())[0]
  216. pdb = pwd.getpwnam(prop.username)
  217. prop.userhome = pdb[5]
  218. prop.history_size = 50
  219. prop.data_dir = os.path.join(prop.home_dir, 'data')
  220. prop.image_dir = os.path.join(prop.home_dir, 'data', 'images')
  221. prop.xml_dir = os.path.join(prop.home_dir, 'data', 'xml')
  222. prop.models_dir = os.path.join(prop.home_dir, 'data', 'models')
  223. prop.localization_dir = os.path.join(prop.home_dir, 'data', 'localization')
  224. prop.max_message_len = 8192
  225. prop.max_message_read = 65536
  226. prop.read_timeout = 90
  227. prop.ppd_search_path = '/usr/share;/usr/local/share;/usr/lib;/usr/local/lib;/usr/libexec;/opt;/usr/lib64'
  228. prop.ppd_search_pattern = 'HP-*.ppd.*'
  229. prop.ppd_download_url = 'http://www.linuxprinting.org/ppd-o-matic.cgi'
  230. prop.ppd_file_suffix = '-hpijs.ppd'
  231. prop.gui_build = to_bool(sys_conf.get('configure', 'gui-build', '0'))
  232. prop.net_build = to_bool(sys_conf.get('configure', 'network-build', '0'))
  233. prop.par_build = to_bool(sys_conf.get('configure', 'pp-build', '0'))
  234. prop.usb_build = True
  235. prop.scan_build = to_bool(sys_conf.get('configure', 'scanner-build', '0'))
  236. prop.fax_build = to_bool(sys_conf.get('configure', 'fax-build', '0'))
  237. prop.doc_build = to_bool(sys_conf.get('configure', 'doc-build', '0'))
  238. prop.foomatic_xml_install = to_bool(sys_conf.get('configure', 'foomatic-xml-install', '0'))
  239. prop.foomatic_ppd_install = to_bool(sys_conf.get('configure', 'foomatic-ppd-install', '0'))
  240. prop.hpcups_build = to_bool(sys_conf.get('configure', 'hpcups-install', '0'))
  241. prop.hpijs_build = to_bool(sys_conf.get('configure', 'hpijs-install', '0'))
  242. spinner = '\\|/-\\|/-'
  243. spinpos = 0
  244.  
  245. def update_spinner():
  246.     global spinpos
  247.     if not log.is_debug() and sys.stdout.isatty():
  248.         sys.stdout.write('\x08' + spinner[spinpos])
  249.         spinpos = (spinpos + 1) % 8
  250.         sys.stdout.flush()
  251.     
  252.  
  253.  
  254. def cleanup_spinner():
  255.     if not log.is_debug() and sys.stdout.isatty():
  256.         sys.stdout.write('\x08 \x08')
  257.         sys.stdout.flush()
  258.     
  259.  
  260. ERROR_STRINGS = {
  261.     ERROR_SUCCESS: 'No error',
  262.     ERROR_UNKNOWN_ERROR: 'Unknown error',
  263.     ERROR_DEVICE_NOT_FOUND: 'Device not found',
  264.     ERROR_INVALID_DEVICE_ID: 'Unknown/invalid device-id field',
  265.     ERROR_INVALID_DEVICE_URI: 'Unknown/invalid device-uri field',
  266.     ERROR_DATA_LENGTH_EXCEEDS_MAX: 'Data length exceeds maximum',
  267.     ERROR_DEVICE_IO_ERROR: 'Device I/O error',
  268.     ERROR_NO_PROBED_DEVICES_FOUND: 'No probed devices found',
  269.     ERROR_DEVICE_BUSY: 'Device busy',
  270.     ERROR_DEVICE_STATUS_NOT_AVAILABLE: 'DeviceStatus not available',
  271.     ERROR_INVALID_SERVICE_NAME: 'Invalid service name',
  272.     ERROR_ERROR_INVALID_CHANNEL_ID: 'Invalid channel-id (service name)',
  273.     ERROR_CHANNEL_BUSY: 'Channel busy',
  274.     ERROR_DEVICE_DOES_NOT_SUPPORT_OPERATION: 'Device does not support operation',
  275.     ERROR_DEVICEOPEN_FAILED: 'Device open failed',
  276.     ERROR_INVALID_DEVNODE: 'Invalid device node',
  277.     ERROR_INVALID_HOSTNAME: 'Invalid hostname ip address',
  278.     ERROR_INVALID_PORT_NUMBER: 'Invalid JetDirect port number',
  279.     ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE: 'No CUPS queue found for device.',
  280.     ERROR_DATFILE_ERROR: 'DAT file error',
  281.     ERROR_INVALID_TIMEOUT: 'Invalid timeout',
  282.     ERROR_IO_TIMEOUT: 'I/O timeout',
  283.     ERROR_FAX_INCOMPATIBLE_OPTIONS: 'Incompatible fax options',
  284.     ERROR_FAX_INVALID_FAX_FILE: 'Invalid fax file',
  285.     ERROR_FAX_FILE_NOT_FOUND: 'Fax file not found',
  286.     ERROR_INTERNAL: 'Unknown internal error' }
  287.  
  288. class Error(Exception):
  289.     
  290.     def __init__(self, opt = ERROR_INTERNAL):
  291.         self.opt = opt
  292.         self.msg = ERROR_STRINGS.get(opt, ERROR_STRINGS[ERROR_INTERNAL])
  293.         log.debug('Exception: %d (%s)' % (opt, self.msg))
  294.         Exception.__init__(self, self.msg, opt)
  295.  
  296.  
  297.  
  298. try:
  299.     True
  300. except NameError:
  301.     True = 1 == 1
  302.     False = not True
  303.  
  304. supported_locales = {
  305.     'en_US': ('us', 'en', 'en_us', 'american', 'america', 'usa', 'english') }
  306.